import math
import random
import time

import gradio
import plotly.express as px
import numpy as np

plot_end = 2 * math.pi

# 定时操作  timer = gradio.Timer(value=1)
# every=1 dome加载完成后每隔1秒执行
with gradio.Blocks() as dome:
    with gradio.Row():
        with gradio.Column():
            md = gradio.Markdown("每秒更新正选曲线")
            slider = gradio.Slider(label="每周期图表", value=1, minimum=1, maximum=10, step=1)
            plot = gradio.Plot(label="展示余弦曲线")


        def get_Plot(zouqi=1):
            global plot_end
            x = np.arange(plot_end - 2 * math.pi, plot_end, 0.02)
            y = np.sin(x * zouqi * 2 * math.pi)
            fig = px.line(x=x, y=y)
            plot_end = plot_end + 2 * math.pi
            return fig


        timer = gradio.Timer(value=1)
        # 定时器触发时更新图表
        timer.tick(fn=get_Plot, inputs=slider, outputs=plot)
        # 滑块变化时更新图表
        dep = dome.load(fn=get_Plot, inputs=None, outputs=plot)
        slider.change(fn=get_Plot, inputs=slider, outputs=plot)
        # dep = dome.load(get_Plot, slider, plot)
        # slider.change(get_Plot, slider, plot)

        # slider.change(fn=get_Plot, inputs=slider, outputs=plot, every=1)

dome.launch()
